//
//  MainTabBarController.swift
//  SwiftWeather
//
//  Created by shen on 2023/8/28.
//

import UIKit

private enum Tabs: Int, CaseIterable {
    case forecast
    case myLocations

    var title: String {
        switch self {
        case .forecast: return Resources.Strings.TabBar.forecast
        case .myLocations: return Resources.Strings.TabBar.myLocations
        }
    }

    var icon: UIImage? {
        switch self {
        case .forecast: return Resources.Images.TabBar.forecast.icon
        case .myLocations: return Resources.Images.TabBar.forecast.selectedIcon
        }
    }

    var selectedIcon: UIImage? {
        switch self {
        case .forecast: return Resources.Images.TabBar.myLocations.icon
        case .myLocations: return Resources.Images.TabBar.myLocations.selectedIcon
        }
    }

    var viewController: UIViewController {
        switch self {
        case .forecast: return CurrentLocationForecastViewController()
        case .myLocations: return MyLocationsViewController()
        }
    }
}

final class MainTabBarController: UITabBarController {
    // MARK: - View Controller Lyfecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        configureAppearance()
        configureViewControllers()
    }

    // MARK: - Methods

    func configureViewControllers() {
        let tabs = Tabs.allCases
        tabs.forEach { setupNavigationController($0) }
    }
}

// MARK: - Private methods

private extension MainTabBarController {
    func configureAppearance() {
        tabBar.tintColor = Resources.Colors.blue
        tabBar.isTranslucent = false
        tabBar.backgroundColor = Resources.Colors.grayBlue
        tabBar.barTintColor = Resources.Colors.grayBlue
    }

    func setupNavigationController(_ tab: Tabs) {
        let navigationController = UINavigationController(rootViewController: tab.viewController)
        navigationController.tabBarItem.title = tab.title
        navigationController.tabBarItem.image = tab.icon
        navigationController.tabBarItem.selectedImage = tab.selectedIcon
        addChild(navigationController)
    }
}
